package com.sanbi.erp.core.util;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @introduce:通过阿里云的文件存储地址，把文件下载到本地
 * @Date: 2022/11/2 14:06
 * @Author: XYP
 */
public class DownLoadFileUtils {

    /**
     * 从网络Url中下载文件
     * @param urlStr url的路径
     * @throws IOException
     */
    public static void  downLoadByUrl(String urlStr,String savePath) {

        try {
            String fileName = getFileName(urlStr);
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //设置超时间为5秒
            conn.setConnectTimeout(5*1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            InputStream inputStream = conn.getInputStream();
            //获取自己数组
            byte[] getData = readInputStream(inputStream);
            //文件保存位置
            File saveDir = new File(savePath);
            if(!saveDir.exists()){ // 没有就创建该文件
                saveDir.mkdir();
            }
            File file = new File(saveDir+File.separator+fileName);
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(getData);

            fos.close();
            inputStream.close();
            System.out.println("the file: "+url+" download success");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 从输入流中获取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    private static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[4*1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

    public static void main(String[] args) {
        try{
            String filePath = "http://192.168.1.118:9091/group1/2022/11/02/20221102124501r7v4i2.xlsx";
//            String filePath = "http://192.168.1.118:9091/group1/2022/11/02/20221102124501r7v4i2.xlsx?download=1";
//            String filePath = "https://docs.spring.io/spring-framework/docs/4.2.0.RC1/spring-framework-reference/pdf//spring-framework-reference.pdf";
//            filePath = "https://docs.spring.io/spring-framework/docs/1.0.0/license.txt";
//            filePath = "https://www.w3school.com.cn/i/eg_tulip.jpg";
//            filePath = "https://www.w3school.com.cn/example/xmle/note.xml";
            downLoadByUrl(filePath,"C:/Users/23026/Desktop/tempQrcode");

        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从src文件路径获取文件名
     * @param srcRealPath src文件路径
     * @return 文件名
     */
    private static String getFileName(String srcRealPath){
        return StringUtils.substringAfterLast(srcRealPath,"/");
    }
}
